package com.winit.openapi.chinapost.constants;

import java.io.File;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.xml.sax.SAXException;

import com.winit.openapi.chinapost.model.LogisticsEventsRequest;
import com.winit.openapi.chinapost.model.ResponsesObject;
import com.winit.openapi.chinapost.model.TracesList;
import com.winit.openapi.exception.ApiException;

/**
 * XML 转换对象
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * zhengbo.huang 	1.0  		2015-4-14 	Created
 * </pre>
 * @since 1.
 */
public class XMLUtils {
    
    static JAXBContext  clearanceJaxbContext;
    static Unmarshaller clearanceJnmarshaller;
    
    static JAXBContext  ctx;
    static Unmarshaller um;
    static {
        String schemaLanguage = XMLConstants.W3C_XML_SCHEMA_NS_URI;
        SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
        Schema schema = null;
        try {
            String xsdFile = XMLUtils.class.getClassLoader().getResource("schema.xsd").getPath();
            schema = schemaFactory.newSchema(new File(xsdFile));
            ctx = JAXBContext.newInstance(LogisticsEventsRequest.class);
            um = ctx.createUnmarshaller();
            um.setSchema(schema);
            clearanceJaxbContext = JAXBContext.newInstance(TracesList.class);
            clearanceJnmarshaller = clearanceJaxbContext.createUnmarshaller();
            
            
        } catch (SAXException e) {
            e.printStackTrace();
            throw new ApiException(ChinaPostErrorCode.ERROR_S01);
        } catch (JAXBException e) {
            e.printStackTrace();
            throw new ApiException(ChinaPostErrorCode.ERROR_S01);
        }
    }

    public static synchronized LogisticsEventsRequest xmlToObject(String inputXmL) {
        LogisticsEventsRequest logisticsEventsRequest = null;
        try {
            logisticsEventsRequest = (LogisticsEventsRequest) um.unmarshal(new StringReader(inputXmL));
        } catch (JAXBException e) {
            e.printStackTrace();
            throw new ApiException(ChinaPostErrorCode.ERROR_S01);
        }
        return logisticsEventsRequest;
    }
    
    public static synchronized TracesList clearanceXmlToObject(String inputXmL) {
        TracesList tracesList = null;
        try {
            tracesList = (TracesList) clearanceJnmarshaller.unmarshal(new StringReader(inputXmL));
        } catch (JAXBException e) {
            e.printStackTrace();
            throw new ApiException(ChinaPostErrorCode.ERROR_S01);
        }
        return tracesList;
    }
    

    public static ResponsesObject objectToXml(ResponsesObject object) {
        try {
            JAXBContext context = JAXBContext.newInstance(ResponsesObject.class);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);
            Writer w = new StringWriter();
            marshaller.marshal(object, w);
        } catch (JAXBException e) {
            // e.printStackTrace();
            throw new ApiException(ChinaPostErrorCode.ERROR_S01);
        }
        return object;
    }

    public static void main(String[] args) {
//        String xml = "<logisticsEventsRequest><logisticsEvent><eventHeader><eventType>LOGIS_CUSTOMS_CLR</eventType><eventTime>2015-01-23 16:37:15</eventTime><eventSource>CPCode</eventSource><eventTarget>wangyitong</eventTarget></eventHeader><eventBody><logisticsDetail><logisticsPlans><logisticsPlan><tradeId>15</tradeId><orderCode>15</orderCode><carrierCode>15</carrierCode><mailNo>15</mailNo><occurtime>2015-01-23 16:37:15</occurtime><remark>15</remark><remarkCode>15</remarkCode></logisticsPlan></logisticsPlans><logisticsPlans><logisticsPlan><tradeId>16</tradeId><orderCode>16</orderCode><carrierCode>16</carrierCode><mailNo>16</mailNo><occurtime>2015-01-23 16:37:15</occurtime><remark>16</remark><remarkCode>16</remarkCode></logisticsPlan></logisticsPlans></logisticsDetail></eventBody></logisticsEvent></logisticsEventsRequest>";
//        LogisticsEventsRequest LogisticsEventsRequest = XMLUtils.xmlToObject(xml);
//        System.out.println(LogisticsEventsRequest.getLogisticsEvent());
                
        String xml1 = "<tracesList><tracesElement><logisticProviderID>Tran_Store_232890</logisticProviderID><mailNos>RA100001009FI</mailNos><txLogisticID>LP00012015280275</txLogisticID><traces><trace><time>2014-12-06 18:00:00</time><desc>Arrived at ShenZhen Yanwen Sorting Center</desc><city>ShenZhen</city><facilityType>1</facilityType><facilityNo>01010001</facilityNo><facilityName>马健</facilityName><action>CAI_GOT</action></trace></traces></tracesElement></tracesList>";
        TracesList tracesList = XMLUtils.clearanceXmlToObject(xml1);
        System.out.println(tracesList);
    }

}
